package com.md24housecall.fibromyalgiadiagnostic;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class QuestionThreeFragment extends Fragment {
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    int questionThreeScore = 0;
    int questionThreeScoreTotal = 0;
    boolean [] checkedCheckBoxs = new boolean [50];
    LinearLayout layout;
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.question_three_fragment, container, false);
        
        String[] items = getResources().getStringArray(R.array.symptoms);
        layout = (LinearLayout)view.findViewById(R.id.list_item);
        
        checkedCheckBoxs = ((MainActivity)getActivity()).getQuestionThreeCheckedCheckBoxs();
        questionThreeScoreTotal = ((MainActivity)getActivity()).getQuestionThreeScore();
        
        int i;
        for(i = 0; i < items.length; i++){
        	CheckBox checkbox = new CheckBox(view.getContext());
        	checkbox.setText(items[i]);
        	checkbox.setChecked(checkedCheckBoxs[i]);
        	if(checkedCheckBoxs[i]){
        		checkbox.setTypeface(null, Typeface.BOLD);
        		checkbox.setTextColor(getResources().getColor(R.color.checked_text));
        		checkbox.setShadowLayer(2, 2, 2, R.color.shadow_text);
        	}else{
        		checkbox.setTypeface(null, Typeface.NORMAL);
        		checkbox.setTextColor(getResources().getColor(R.color.unchecked_text));
        		checkbox.setShadowLayer(0, 0, 0, R.color.shadow_text);
        	}
        	
        	checkbox.setOnClickListener(new OnClickListener(){
        		public void onClick(View v){
        			onQuestionThreeCheckboxClicked(v);
        		}
        	});
        	layout.addView(checkbox, i);
        }
        Button button = (Button) view.findViewById(R.id.next_button);
        button.setOnClickListener((OnClickListener) new ButtonEvent());

		return view;
    }

    public void onQuestionThreeCheckboxClicked(View v){
    	
    	CheckBox checkbox = (CheckBox) v;
    	int index = layout.indexOfChild(v);
    	if(checkbox.isChecked()){
    		questionThreeScoreTotal += 1;
    		checkedCheckBoxs[index] = true;
    		checkbox.setTypeface(null, Typeface.BOLD);
    		checkbox.setTextColor(getResources().getColor(R.color.checked_text));
    		checkbox.setShadowLayer(2, 2, 2, R.color.shadow_text);
        }else{
        	questionThreeScoreTotal -= 1;
        	checkedCheckBoxs[index] = false;
        	checkbox.setTypeface(null, Typeface.NORMAL);
    		checkbox.setTextColor(getResources().getColor(R.color.unchecked_text));
    		checkbox.setShadowLayer(0, 0, 0, R.color.shadow_text);
        }
    	
    	((MainActivity)getActivity()).setQuestionThreeScore(questionThreeScoreTotal);
    	
    	
    	
    }
    
    private class ButtonEvent implements OnClickListener{

        @Override
        public void onClick(View v) {
        	ResultFragment newFragment = new ResultFragment();
            Bundle args = new Bundle();
            //args.putInt("question_three_score", questionThreeScore);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.in_from_right, R.anim.out_to_left, R.anim.in_from_left, R.anim.out_to_right);

            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null).commit();
        }

    }
    
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
           // updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            //updateArticleView(mCurrentPosition);
        }
    }

   
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}